package com.sportschain.app.core.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;

/**
 * ViewPager指示器
 *
 * @author LYZ 2019.01.12
 */
public class ViewPagerIndicator extends View {

    private Context mContext;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private ViewPager mViewPager;


    private int mIndicatorRadius = 5;
    private int mIndicatorMargin = 5;
    private int mIndicatorLineWidth = 1;
    private int mSelectedPos = 0;

    private int mIndicatorColor;

    public ViewPagerIndicator(Context context) {
        this(context, null);
    }

    public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewPagerIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttr();
    }


    private void initAttr() {
        mIndicatorColor = Color.BLACK;
        mPaint.setColor(mIndicatorColor);

        mIndicatorRadius = ConvertUtils.dp2px(mIndicatorRadius);
        mIndicatorMargin = ConvertUtils.dp2px(mIndicatorMargin);
        mIndicatorLineWidth = ConvertUtils.dp2px(mIndicatorLineWidth);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), mIndicatorRadius * 2 + mIndicatorLineWidth * 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mViewPager != null
                && mViewPager.getAdapter() != null
                && mViewPager.getAdapter().getCount() != 0) {

            int width = getMeasuredWidth();
            int center = width / 2;


            //绘制指示器
            int indicatorStart = center - ((mIndicatorRadius * 2 + mIndicatorMargin) * (mViewPager.getAdapter().getCount() - 1)) / 2;
            for (int i = 0, size = mViewPager.getAdapter().getCount(); i < size; i++, indicatorStart += (mIndicatorRadius * 2 + mIndicatorMargin)) {
                if (i == mSelectedPos) {
                    mPaint.setStyle(Paint.Style.FILL);
                } else {
                    mPaint.setStyle(Paint.Style.STROKE);
                    mPaint.setStrokeWidth(mIndicatorLineWidth);
                }
                canvas.drawCircle(indicatorStart, mIndicatorRadius + mIndicatorLineWidth, mIndicatorRadius, mPaint);

            }
        }


    }

    public void attachViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mSelectedPos = position;
                ViewPagerIndicator.this.invalidate();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public void setIndicatorRadius(int indicatorRadius) {
        mIndicatorRadius = ConvertUtils.dp2px(indicatorRadius);
    }

    public void setIndicatorMargin(int indicatorMargin) {
        mIndicatorMargin = ConvertUtils.dp2px(indicatorMargin);
    }

    public void setIndicatorColor(int indicatorColor) {
        mIndicatorColor = indicatorColor;
        mPaint.setColor(mIndicatorColor);
    }
}